package com.vstudy.zx.controller;

import com.vstudy.zx.entity.Article;
import com.vstudy.zx.entity.Teacher;
import com.vstudy.zx.service.MainService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.List;

@Controller
public class MainController {
    @Autowired
    private MainService mainService;

    @RequestMapping(value = {"/index", "/index.do", ""})
    public String index(HttpServletRequest request, Model model) {
        HttpSession session = request.getSession();
        Teacher teacher = new Teacher();
        teacher.setTe_id(1L);
        teacher.setTe_password("123");
        teacher.setTe_name("李虎");
        teacher.setTe_login_name("李虎");
        session.setAttribute("teacher", teacher);

        // 提供数据，最近动态
//        List<Article> articles = mainService.getRecentArticles(20);

        return "index.html";
    }

    @CrossOrigin(origins = "*", maxAge = 3600)
    @RequestMapping(value = "/getArticles", method = RequestMethod.POST)
    @ResponseBody
    public Object getArticles(Integer pageSize, Integer pageIndex, HttpServletRequest request) {
        List<Article> articles = mainService.getRecentArticles(pageIndex, pageSize, request);
        return articles;
    }

    @GetMapping("test")
    @ResponseBody
    public Object test(HttpServletRequest request) {
        String path = null;
        try {
            path = ResourceUtils.getURL("classpath:").getPath();
            File file = new File(path, "static/js/index.js");
            System.out.println();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        System.out.println(path);

        return "hello";
    }
}
